package com.yaoyanshe.trialfield.common.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationManagerCompat;

/**
 * <p>description:</p>
 * <p>created on: 2020/7/16 11:09</p>
 *
 * @author tck
 * @version v1.0.0
 */
public class NotificationsUtils {

    private static final String TAG = "tck6666";

    public static final String KEY_CHANNEL_ID = "1";

    private NotificationsUtils() {

    }

    public static boolean isNotificationEnabled(@NonNull Context context) {
        NotificationManagerCompat from = NotificationManagerCompat.from(context);
        return from.areNotificationsEnabled();
    }

    public static void openNotification(@NonNull Context context) {
        openNotification(context, "");
    }

    public static void openNotification(@NonNull Context context, String channelId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                openNotificationByChannelId(context, channelId);
            } else {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                context.startActivity(intent);
            }
        } catch (Exception e) {
            Log.d(TAG, "openNotification error,channelId:" + channelId);
        }
    }

    /**
     * 直接打开对应渠道Id的设置界面
     *
     * @param channelId
     */
    @RequiresApi(Build.VERSION_CODES.O)
    public static void openNotificationByChannelId(@NonNull Context context, String channelId) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (!TextUtils.isEmpty(channelId) && !isNotificationChannelEnabled(context, channelId)) {
                intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
            } else {
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
            }
            context.startActivity(intent);
        } catch (Exception e) {
            Log.d(TAG, "openNotificationByChannelId error,channelId:" + channelId);
        }
    }


    public static boolean isNotificationChannelEnabled(@NonNull Context context, String channelId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !TextUtils.isEmpty(channelId)) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
                if (notificationChannel != null) {
                    return notificationChannel.getImportance() != NotificationManager.IMPORTANCE_NONE;
                }
            }
        }
        return true;
    }

    /**
     * 判断推送关闭 并且 推送渠道是否关闭
     * @param context
     * @param channelId
     * @return
     */
    public static boolean isNotificationAndChannelEnabled(@NonNull Context context, String channelId) {
        boolean notificationEnabled = isNotificationEnabled(context);
        if (notificationEnabled) {
            return isNotificationChannelEnabled(context, channelId);
        }
        return false;
    }

}
